﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using BusinessLogic;
using SEP.Admin;
using DataAccessLogic;
using System.Text;

public partial class Admin_ConstituentInformation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            
            string name = "No constituent was selected.";
            
            Constituent constituent = GetCurrentConstituent();

            if(constituent != null)
            {
                hyperCreateJob.NavigateUrl = string.Format("~/Admin/ServiceReportJobCreator.aspx?constituentID={0}", constituent.ID);

                name = constituent.Name;                    
                    
                if(string.IsNullOrWhiteSpace(name))
                {
                    name = "Name isn't currently available.";
                }  

                this.lblNameRelationship.Text = name;

                ViewState[SEPDefaults.ViewStateKeyNames.CONSTITUENT] = constituent; 
                    
                this.BindDropDownLists();
                this.BindGridView();
                
                List<string> constituentNames = MyCache.CheckCache<List<string>>(SEPDefaults.CacheKeyNames.CONSTITUENTNAMES);

                if(null == constituentNames)
                {                
                    constituentNames = Constituent.GetActiveNames();
                    MyCache.AddToCache(SEPDefaults.CacheKeyNames.CONSTITUENTNAMES, constituentNames);
                }

                string script = CreateStringBuilderScript(constituentNames, name);
                ClientScript.RegisterStartupScript(this.GetType(), "AutoCompleteSearch", script, true);                

                this.SetDefaults();
            }
            
            lblName.Text = name;                                    
        }

        BuildBackLink();
        ClientScript.RegisterStartupScript(this.GetType(), "PhoneModalPopup", "function showPhoneModal() { $find('ShowPhoneModal').show(); }", true);
        ClientScript.RegisterStartupScript(this.GetType(), "EmailModalPopup", "function showEmailModal() { $find('ShowEmailModal').show(); }", true);
        ClientScript.RegisterStartupScript(this.GetType(), "AddressModalPopup", "function showAddressModal() { $find('ShowAddressModal').show(); }", true);
        ClientScript.RegisterStartupScript(this.GetType(), "RelationshipModalPopup", "function showRelationshipModal() { $find('ShowRelationshipModal').show(); }", true);
        ClientScript.RegisterStartupScript(this.GetType(), "NoteModalPopup", "function showNoteModal() { $find('ShowNoteModal').show(); }", true);
    }    

    private void SetDefaults()
    {
        Constituent constituent = GetCurrentConstituent();

        if (constituent.EmailAddresses == null || constituent.EmailAddresses.Count < 1 || (constituent.EmailAddresses.Select(e => e.IsPrimary == true)).Count() < 1)
        {
            chkEmailIsPrimary.Checked = true;
        }

        if (constituent.Phones == null || constituent.Phones.Count < 1 || (constituent.Phones.Select(p => p.IsPrimary == true)).Count() < 1)
        {
            chkPhoneIsPrimary.Checked = true;
        }

        if (constituent.Addresses == null || constituent.Addresses.Count < 1 || (constituent.Addresses.Select(a => a.IsPrimary == true)).Count() < 1)
        {
            chkAddressIsPrimary.Checked = true;
        }
    }

    private Constituent GetCurrentConstituent()
    {
        Constituent constituent = null;
        
        if(null != ViewState[SEPDefaults.ViewStateKeyNames.CONSTITUENT])
        {
            constituent = (Constituent)ViewState[SEPDefaults.ViewStateKeyNames.CONSTITUENT];
        }
        else
        {
            if(Request.QueryString["ID"] != null)
            {
                string constituentID = Request.QueryString["ID"].ToString();
            
                if(!string.IsNullOrWhiteSpace(constituentID))
                {
                    constituent = new Constituent(new Guid(constituentID)); 
                }
            }
        }        

        return constituent;
    }
        
    private void BuildBackLink()
    {
        var url = "~/Admin/ConstituentManagement.aspx?pageSize={0}&searchText={1}&pageIndex={2}&fromBack=true";
        var pageSize = string.Empty;
        var searchText = string.Empty;
        var pageIndex = string.Empty;

        if (Request.QueryString["pageSize"] != null)
        {
            pageSize = Request.QueryString["pageSize"].ToString();
        }

        if (Request.QueryString["searchText"] != null)
        {
            searchText = Request.QueryString["searchText"].ToString();
        }

        if (Request.QueryString["pageIndex"] != null)
        {
            pageIndex = Request.QueryString["pageIndex"].ToString();
        }

        hyperBack.NavigateUrl = string.Format(url, pageSize, searchText, pageIndex);
    }

    private string CreateStringBuilderScript(List<string> constituentNames, string name)
    {
        StringBuilder script = new StringBuilder();
        script.AppendLine("$(function() {");
        script.AppendLine("var availableTags = [");

        foreach (var item in constituentNames)
        {
            if(!item.Equals(name))
            {
                script.AppendLine(string.Format("'{0}'", item.Replace("'", "")));

                if (item != constituentNames[constituentNames.Count - 1])
                {
                    script.Append(",");
                }
            }
        }

        script.AppendLine("];");
        script.AppendLine("$( '#" + txtAutoComplete.ClientID + "' ).autocomplete({");
        script.AppendLine("source: availableTags");
        script.AppendLine("});");
        script.AppendLine("});");

        return script.ToString();
    }

    private void BindGridView(Constituent.Refresh refresh)
    {
        Constituent constituent = GetCurrentConstituent();
        constituent.RefreshTables(refresh);
        BindGridView(constituent);
    }

    private void BindGridView()
    {
        BindGridView(null);
    }

    private void BindGridView(Constituent constituent)
    {
        if(constituent == null)
        {
            constituent = GetCurrentConstituent();
        }

        Common.Utility.BindGridView<Address>(grvAddresses, constituent.Addresses);        
        Common.Utility.BindGridView<Phone>(grvPhones, constituent.Phones);
        Common.Utility.BindGridView<Email>(grvEmails, constituent.EmailAddresses);
        Common.Utility.BindGridView<Relationship>(grvRelationships, constituent.Relationships);
        Common.Utility.BindGridView<Note>(grvNotes, constituent.Notes);
    }

    private void BindDropDownLists()
    {
        Common.Utility.BindDropDownList<AddressType>(ddlAddressType, MyCache.GetCodeTableCache<AddressType>(), SEPDefaults.Other.AddressType);
        Common.Utility.BindDropDownList<EmailType>(ddlEmailType, MyCache.GetCodeTableCache<EmailType>(), SEPDefaults.Other.EmailType);
        Common.Utility.BindDropDownList<PhoneType>(ddlPhoneType, MyCache.GetCodeTableCache<PhoneType>(), SEPDefaults.Other.PhoneType);
        Common.Utility.BindDropDownList<State>(ddlState, MyCache.GetCodeTableCache<State>(), SEPDefaults.Other.State);
        Common.Utility.BindDropDownList<Country>(ddlCountry, MyCache.GetCodeTableCache<Country>(), SEPDefaults.Other.CountryAbbreviation, "ABBREVIATION", "ID");
        Common.Utility.BindDropDownList<RelationshipType>(ddlRelationshipTypes, MyCache.GetCodeTableCache<RelationshipType>(), SEPDefaults.Other.RelationshipTypeEmployee);
        Common.Utility.BindDropDownList<RelationshipType>(ddlReciprocalRelationshipTypes, MyCache.GetCodeTableCache<RelationshipType>(), SEPDefaults.Other.RelationshipTypeEmployer);
    }

    protected void btnNewRelationship_Click(object sender, EventArgs e)
    {
        pnlNewRelationship.Visible = true;
        modalNewRelationship.Show();
    }

    protected void btnRelationshipSubmit_Click(object sender, EventArgs e)
    {        
        string name = txtAutoComplete.Text;

        if(!string.IsNullOrWhiteSpace(name))
        {
            List<Constituent> constituents = Constituent.Search(name);

            if(null != constituents && 0 < constituents.Count)
            {
                Constituent c = constituents[0];

                Relationship relationship = new Relationship();
                relationship.IsPrimary = chkRelationshipIsPrimary.Checked;
                relationship.RelationshipConstituent = GetCurrentConstituent(); 
                relationship.ReciprocalConstituent = c;
                relationship.RelationshipTypeCode = GetRelationshipTypeFromDropDownList(ddlRelationshipTypes);
                relationship.ReciprocalRelationshipTypeCode = GetRelationshipTypeFromDropDownList(ddlReciprocalRelationshipTypes);

                if(relationship.Create())
                {
                    this.BindGridView(Constituent.Refresh.Relationship);
                    txtAutoComplete.Text = string.Empty;
                    lblRelationshipError.Visible = false;
                }
                else
                {
                    lblRelationshipError.Visible = true;
                    lblRelationshipError.Text = SEPDefaults.ErrorMessages.GENERIC;
                }
            }
        }
    }

    private RelationshipType GetRelationshipTypeFromDropDownList(DropDownList ddl)
    {
        RelationshipType rt = new RelationshipType();
        rt.ID = new Guid(ddl.SelectedItem.Value);
        rt.Description = ddl.SelectedItem.Text;
        return rt;
    }

    private void UpdateConstituent(Constituent constituent)
    {
        ConstituentDataAccess.Update(constituent);
        ViewState[SEPDefaults.ViewStateKeyNames.CONSTITUENT] = constituent;
    }

    #region Address
    protected void grvAddresses_CancelRecord(object sender, GridViewCancelEditEventArgs e)
    {
        SetIndex(grvAddresses, -1);
    }

    protected void grvAddresses_EditRecord(object sender, GridViewEditEventArgs e)
    {
        SetIndex(grvAddresses, e.NewEditIndex);
    }

    protected void grvAddresses_UpdateRecord(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = grvAddresses.Rows[e.RowIndex];

        if (row != null)
        {
            Address a = new Address();
            a.ID = new Guid(grvAddresses.DataKeys[e.RowIndex].Value.ToString());

            TextBox txtAddressBlock = (TextBox)row.FindControl("txtAddressBlock");
            a.AddressBlock = txtAddressBlock.Text;

            TextBox txtCity = (TextBox)row.FindControl("txtCity");
            a.City = txtCity.Text;

            TextBox txtPostCode = (TextBox)row.FindControl("txtPostCode");
            a.PostCode = txtPostCode.Text;

            CheckBox chkIsPrimary = (CheckBox)row.FindControl("chkIsPrimary");
            a.IsPrimary = chkIsPrimary.Checked;

            DropDownList ddlAddressType = (DropDownList)row.FindControl("ddlAddressType");
            a.AddressType = new AddressType();
            a.AddressType.ID = new Guid(ddlAddressType.SelectedValue);

            DropDownList ddlState = (DropDownList)row.FindControl("ddlState");
            a.State = new State();
            a.State.ID = new Guid(ddlState.SelectedValue);

            DropDownList ddlCountry = (DropDownList)row.FindControl("ddlCountry");
            a.Country = new Country();
            a.Country.ID = new Guid(ddlCountry.SelectedValue);

            a.Update(GetCurrentConstituent().ID);

            grvAddresses.EditIndex = -1;
            BindGridView(Constituent.Refresh.Address);
        }
    }

    protected void grvAddresses_DeleteRecord(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = grvAddresses.Rows[e.RowIndex];

        if (row != null)
        {
            Address a = new Address();
            a.ID = new Guid(grvAddresses.DataKeys[e.RowIndex].Value.ToString());
            a.Delete();

            BindGridView(Constituent.Refresh.Address);
        }
    }

    protected void grvAddresses_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        RowDataBound(e, "address");

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlState = (DropDownList)e.Row.FindControl("ddlState");
            
            if (ddlState != null)
            {
                ddlState.DataTextField = "ABBREVIATION";
                ddlState.DataValueField = "ID";
                ddlState.DataSource = new State().GetAll();
                ddlState.DataBind();

                string type = ((Label)e.Row.FindControl("lblState")).Text;

                if (!string.IsNullOrWhiteSpace(type))
                {
                    ddlState.SelectedIndex = ddlState.Items.IndexOf(ddlState.Items.FindByText(type));
                }
            }

            DropDownList ddlAddressType = (DropDownList)e.Row.FindControl("ddlAddressType");

            if (ddlAddressType != null)
            {
                ddlAddressType.DataTextField = "DESCRIPTION";
                ddlAddressType.DataValueField = "ID";
                ddlAddressType.DataSource = new AddressType().GetAll();
                ddlAddressType.DataBind();

                string type = ((Label)e.Row.FindControl("lblAddressType")).Text;

                if (!string.IsNullOrWhiteSpace(type))
                {
                    ddlAddressType.SelectedIndex = ddlAddressType.Items.IndexOf(ddlAddressType.Items.FindByText(type));
                }
            }

            DropDownList ddlCountry = (DropDownList)e.Row.FindControl("ddlCountry");

            if (ddlCountry != null)
            {
                ddlCountry.DataTextField = "ABBREVIATION";
                ddlCountry.DataValueField = "ID";
                ddlCountry.DataSource = new Country().GetAll();
                ddlCountry.DataBind();

                string type = ((Label)e.Row.FindControl("lblCountry")).Text;

                if (!string.IsNullOrWhiteSpace(type))
                {
                    ddlCountry.SelectedIndex = ddlCountry.Items.IndexOf(ddlCountry.Items.FindByText(type));
                }
            }
        }
    }

    protected void btnNewAddress_Click(object sender, EventArgs e)
    {
        pnlNewAddress.Visible = true;
        modalNewAddress.Show();
    }

    protected void btnAddressSubmit_Click(object sender, EventArgs e)
    {
        string address = txtAddress.Text;

        if (!string.IsNullOrWhiteSpace(address))
        {
            Constituent constituent = GetCurrentConstituent();

            Address a = new Address();
            a.AddressBlock = address;
            a.City = txtCity.Text;
            a.PostCode = txtZip.Text;
            a.IsPrimary = chkAddressIsPrimary.Checked;

            AddressType at = new AddressType();
            at.ID = new Guid(ddlAddressType.SelectedValue);
            a.AddressType = at;

            State s = new State();
            s.ID = new Guid(ddlState.SelectedValue);
            a.State = s;

            Country c = new Country();
            c.ID = new Guid(ddlCountry.SelectedValue);
            a.Country = c;

            if (a.Create(constituent.ID))
            {
                this.BindGridView(Constituent.Refresh.Address);
                this.SetDefaults();

                foreach (var item in pnlNewAddress.Controls)
                {
                    try
                    {
                        TextBox t = (TextBox)item;
                        t.Text = string.Empty;
                    }
                    catch { }

                    try
                    {
                        DropDownList ddl = (DropDownList)item;
                        ddl.SelectedIndex = 0;
                    }
                    catch { }
                }
                lblEmailError.Visible = false;
            }
            else
            {
                lblEmailError.Visible = true;
                lblEmailError.Text = SEPDefaults.ErrorMessages.GENERIC;
            }
        }
    }
    #endregion

    #region Phone
    protected void grvPhones_CancelRecord(object sender, GridViewCancelEditEventArgs e)
    {
        SetIndex(grvPhones, -1);
    }

    protected void grvPhones_EditRecord(object sender, GridViewEditEventArgs e)
    {
        SetIndex(grvPhones, e.NewEditIndex);
    }

    protected void grvPhones_UpdateRecord(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = grvPhones.Rows[e.RowIndex];

        if(row != null)
        {            
            Phone p = new Phone();
            
            p.ID = new Guid(grvPhones.DataKeys[e.RowIndex].Value.ToString());

            CheckBox chkIsPrimary = (CheckBox)row.FindControl("chkIsPrimary");
            p.IsPrimary = chkIsPrimary.Checked;

            TextBox txtNumber = (TextBox)row.FindControl("txtNumber");
            p.Number = txtNumber.Text;
            
            TextBox txtNumberExtension = (TextBox)row.FindControl("txtNumberExtension");
            p.Extension = txtNumberExtension.Text;

            PhoneType pt = new PhoneType();
            DropDownList ddlPhoneType = (DropDownList)row.FindControl("ddlPhoneType");
            pt.ID = new Guid(ddlPhoneType.SelectedValue.ToString());

            p.PhoneType = pt;

            p.Update(GetCurrentConstituent().ID);

            grvPhones.EditIndex = -1;
            BindGridView(Constituent.Refresh.Phone);
        }        
    }

    protected void grvPhones_DeleteRecord(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = grvPhones.Rows[e.RowIndex];

        if (row != null)
        {
            Phone p = new Phone();
            p.ID = new Guid(grvPhones.DataKeys[e.RowIndex].Value.ToString());
            p.Delete();

            BindGridView(Constituent.Refresh.Phone);
        }        
    }

    protected void grvPhones_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        RowDataBound(e, "phone");

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList grvDdlPhoneType = (DropDownList)e.Row.FindControl("ddlPhoneType");

            if(grvDdlPhoneType != null)
            {
                grvDdlPhoneType.DataTextField = "DESCRIPTION";
                grvDdlPhoneType.DataValueField = "ID";
                grvDdlPhoneType.DataSource = new PhoneType().GetAll();
                grvDdlPhoneType.DataBind();

                string type = ((Label)e.Row.FindControl("lblPhoneType")).Text;

                if(!string.IsNullOrWhiteSpace(type))
                {
                    grvDdlPhoneType.SelectedIndex = grvDdlPhoneType.Items.IndexOf(grvDdlPhoneType.Items.FindByText(type));
                }
            }
        }
    }

    protected void btnNewPhone_Click(object sender, EventArgs e)
    {
        pnlNewPhone.Visible = true;
        modalNewPhone.Show();
    }

    protected void btnPhoneSubmit_Click(object sender, EventArgs e)
    {
        string phone = txtPhone.Text;
        string ext = txtExtension.Text;

        if (!string.IsNullOrWhiteSpace(phone) || !string.IsNullOrWhiteSpace(ext))
        {
            Constituent constituent = GetCurrentConstituent();

            Phone p = new Phone();
            p.Number = phone;
            p.Extension = ext;
            p.IsPrimary = chkPhoneIsPrimary.Checked;

            PhoneType pt = new PhoneType();
            pt.ID = new Guid(ddlPhoneType.SelectedValue);
            p.PhoneType = pt;

            if (p.Create(constituent.ID))
            {
                this.BindGridView(Constituent.Refresh.Phone);
                this.SetDefaults();

                txtPhone.Text = string.Empty;
                txtExtension.Text = string.Empty;
                chkPhoneIsPrimary.Checked = false;
                ddlPhoneType.SelectedIndex = 0;
                lblPhoneError.Visible = false;
            }
            else
            {
                lblPhoneError.Visible = true;
                lblPhoneError.Text = SEPDefaults.ErrorMessages.GENERIC;
            }
        }
    }
    #endregion

    #region Email
    protected void grvEmails_CancelRecord(object sender, GridViewCancelEditEventArgs e)
    {
        SetIndex(grvEmails, -1);
    }

    protected void grvEmails_EditRecord(object sender, GridViewEditEventArgs e)
    {
        SetIndex(grvEmails, e.NewEditIndex);
    }

    protected void grvEmails_UpdateRecord(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = grvEmails.Rows[e.RowIndex];

        if (row != null)
        {
            Email em = new Email();

            em.ID = new Guid(grvEmails.DataKeys[e.RowIndex].Value.ToString());
            
            CheckBox chkIsPrimary = (CheckBox)row.FindControl("chkIsPrimary");
            em.IsPrimary = chkIsPrimary.Checked;

            TextBox txtEmailAddress = (TextBox)row.FindControl("txtEmailAddress");
            em.EmailAddress = txtEmailAddress.Text;            
            
            EmailType et = new EmailType();
            DropDownList ddlEmailAddressType = (DropDownList)row.FindControl("ddlEmailAddressType");
            et.ID = new Guid(ddlEmailAddressType.SelectedValue.ToString());

            em.EmailAddressType = et;

            em.Update(GetCurrentConstituent().ID);

            grvEmails.EditIndex = -1;
            BindGridView(Constituent.Refresh.Email);
        }
    }

    protected void grvEmails_DeleteRecord(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = grvEmails.Rows[e.RowIndex];

        if (row != null)
        {
            Email em = new Email();
            em.ID = new Guid(grvEmails.DataKeys[e.RowIndex].Value.ToString());
            em.Delete();

            BindGridView(Constituent.Refresh.Email);
        }
    }

    protected void grvEmails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        RowDataBound(e, "email");

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList grvDdlEmailAddressType = (DropDownList)e.Row.FindControl("ddlEmailAddressType");

            if (grvDdlEmailAddressType != null)
            {
                grvDdlEmailAddressType.DataTextField = "DESCRIPTION";
                grvDdlEmailAddressType.DataValueField = "ID";
                grvDdlEmailAddressType.DataSource = new EmailType().GetAll();
                grvDdlEmailAddressType.DataBind();

                string type = ((Label)e.Row.FindControl("lblEmailAddressType")).Text;

                if (!string.IsNullOrWhiteSpace(type))
                {
                    grvDdlEmailAddressType.SelectedIndex = grvDdlEmailAddressType.Items.IndexOf(grvDdlEmailAddressType.Items.FindByText(type));
                }
            }
        }
    }
    
    protected void btnNewEmail_Click(object sender, EventArgs e)
    {
        pnlNewEmail.Visible = true;
        modalNewEmail.Show();
    }

    protected void btnEmailSubmit_Click(object sender, EventArgs e)
    {
        string emailAddress = txtEmail.Text;

        if (!string.IsNullOrWhiteSpace(emailAddress))
        {
            Constituent constituent = GetCurrentConstituent();

            Email em = new Email();
            em.EmailAddress = emailAddress;
            em.IsPrimary = chkEmailIsPrimary.Checked;

            EmailType et = new EmailType();
            et.ID = new Guid(ddlEmailType.SelectedValue);
            em.EmailAddressType = et;

            if (em.Create(constituent.ID))
            {
                this.BindGridView(Constituent.Refresh.Email);
                this.SetDefaults();

                txtEmail.Text = string.Empty;
                chkEmailIsPrimary.Checked = false;
                ddlEmailType.SelectedIndex = 0;
                lblEmailError.Visible = false;
            }
            else
            {
                lblEmailError.Visible = true;
                lblEmailError.Text = SEPDefaults.ErrorMessages.GENERIC;
            }
        }
    }
    #endregion

    #region Note
    protected void grvNotes_CancelRecord(object sender, GridViewCancelEditEventArgs e)
    {
        SetIndex(grvNotes, -1);
    }

    protected void grvNotes_EditRecord(object sender, GridViewEditEventArgs e)
    {
        SetIndex(grvNotes, e.NewEditIndex);
    }

    protected void grvNotes_UpdateRecord(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = grvNotes.Rows[e.RowIndex];

        if (row != null)
        {
            Note n = new Note();
            n.ID = new Guid(grvNotes.DataKeys[e.RowIndex].Value.ToString());
                        
            TextBox txtNote = (TextBox)row.FindControl("txtNote");
            n.Text = txtNote.Text;
            n.ConstituentID = GetCurrentConstituent().ID;
            n.Update();

            grvNotes.EditIndex = -1;
            BindGridView(Constituent.Refresh.Note);
        }
    }

    protected void grvNotes_DeleteRecord(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = grvNotes.Rows[e.RowIndex];

        if (row != null)
        {
            Note n = new Note();
            n.ID = new Guid(grvNotes.DataKeys[e.RowIndex].Value.ToString());
            n.Delete();

            BindGridView(Constituent.Refresh.Note);
        }
    }

    protected void grvNotes_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        RowDataBound(e, "note");        
    }

    protected void btnNewNote_Click(object sender, EventArgs e)
    {
        pnlNewNote.Visible = true;
        modalNewNote.Show();
    }

    protected void btnNoteSubmit_Click(object sender, EventArgs e)
    {
        string text = txtNewNote.Text;

        if (!string.IsNullOrWhiteSpace(text))
        {
            Note note = new Note();
            note.Text = text;
            note.Constituent = GetCurrentConstituent();

            if (note.Create())
            {
                this.BindGridView(Constituent.Refresh.Note);
                txtNewNote.Text = string.Empty;
                lblNoteError.Visible = false;
            }
            else
            {
                lblNoteError.Visible = true;
                lblNoteError.Text = SEPDefaults.ErrorMessages.GENERIC;
            }
        }
    }
    #endregion

    private void SetIndex(GridView grv, int editIndex)
    {
        grv.EditIndex = editIndex;
        this.BindGridView();
    }

    /// <summary>
    /// Adds onclick to the linkbutton in the gridview.
    /// </summary>
    /// <param name="e"></param>
    /// <param name="deleteWhat">You are about to delete this [deleteWhat]?</param>
    private void RowDataBound(GridViewRowEventArgs e, string deleteWhat)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
            l.Attributes.Add("onclick", string.Format("javascript:return confirm('You are about to delete this {0}?')", deleteWhat.Trim()));
        }
    }
} 